<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="busiType">
        <el-select
          v-model="queryParams.busiType"
          placeholder="请选择业务类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="addPurPlan"
          >新建</el-button
        >
      </el-form-item>
    </el-form>

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="待提交" name="first">
        <el-table
          v-loading="loading"
          :data="pursList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- 添加序号列 -->
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center"
            index-method="indexMethod"
          />

          <el-table-column label="计划编码" align="center" prop="planCode" />
          <el-table-column label="计划名称" align="center" prop="planName" width="200px">
            <template slot-scope="scope">
              <div
                style="
                  color: black;
                  font-weight: bold;
                  font-family: '仿宋';
                  font-size: 15px;
                "
                class="planName"
              >
                {{ scope.row.planName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="业务类型" align="center" prop="busiType">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.business_type"
                :value="scope.row.busiType"
              />
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="creater" />
          <el-table-column label="创建部门" align="center" prop="deptName" />
          <el-table-column label="创建时间" align="center" prop="creatTime">
            <template slot-scope="scope">
              {{ myFormatTime(scope.row.creatTime) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="statu">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.purs_statu"
                :value="scope.row.status"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="recordsDetails(scope.row)"
                v-hasPermi="['purs:purs:edit']"
                >审批记录</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['purs:purs:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['purs:purs:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="待审批" name="second">
        <el-table
          v-loading="loading"
          :data="pursList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- 添加序号列 -->
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center"
            index-method="indexMethod"
          />
          <el-table-column label="计划编码" align="center" prop="planCode" />
          <el-table-column label="计划名称" align="center" prop="planName" width="200px">
            <template slot-scope="scope">
              <div
                style="
                  color: black;
                  font-weight: bold;
                  font-family: '仿宋';
                  font-size: 15px;
                "
                class="planName"
              >
                {{ scope.row.planName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="业务类型" align="center" prop="busiType">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.business_type"
                :value="scope.row.busiType"
              />
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="creater" />
          <el-table-column label="创建部门" align="center" prop="deptName" />
          <el-table-column label="创建时间" align="center" prop="creatTime">
            <template slot-scope="scope">
              {{ myFormatTime(scope.row.creatTime) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="statu">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.purs_statu"
                :value="scope.row.status"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="checkPlan(scope.row)"
                >审批记录</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['purs:purs:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="已生效" name="third">
        <el-table
          v-loading="loading"
          :data="pursList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- 添加序号列 -->
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center"
            index-method="indexMethod"
          />
          <el-table-column label="计划编码" align="center" prop="planCode" />
          <el-table-column label="计划名称" align="center" prop="planName" width="200px">
            <template slot-scope="scope">
              <div
                style="
                  color: black;
                  font-weight: bold;
                  font-family: '仿宋';
                  font-size: 15px;
                "
                class="planName"
              >
                {{ scope.row.planName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="业务类型" align="center" prop="busiType">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.business_type"
                :value="scope.row.busiType"
              />
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="creater" />
          <el-table-column label="创建部门" align="center" prop="deptName" />
          <el-table-column label="创建时间" align="center" prop="creatTime">
            <template slot-scope="scope">
              {{ myFormatTime(scope.row.creatTime) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="statu">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.purs_statu"
                :value="scope.row.status"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="recordsDetails(scope.row)"
                v-hasPermi="['purs:purs:edit']"
                >审批记录</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改采购计划.审批通过后生成招标项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="业务类型" prop="busiType">
          <el-select v-model="form.busiType" placeholder="请选择业务类型">
            <el-option
              v-for="dict in dict.type.business_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建部门" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入创建部门" />
        </el-form-item>
        <el-form-item label="上传附件" prop="loadFileId">
          <el-input v-model="form.loadFileId" placeholder="请输入上传附件" />
        </el-form-item>
        <el-form-item label="采购方式" prop="prodMethod">
          <el-input v-model="form.prodMethod" placeholder="请输入采购方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="提示"
      :visible.sync="checkdialogVisible"
      width="62%"
      :before-close="handleClose"
    >
      <div class="check-box">
        <el-tabs type="border-card">
          <el-tab-pane>
            <span slot="label"><i class="el-icon-date"></i> 采购信息</span>
            <div class="budGetInfo-box">
              <el-table :data="budgetList" border>
                <el-table-column label="预算编号" align="center" prop="bgId" />
                <el-table-column
                  label="部门名称"
                  align="center"
                  prop="createPost"
                />
                <el-table-column
                  label="预算名称"
                  align="center"
                  prop="bgName"
                  width="170px"
                />

                <el-table-column
                  label="总金额"
                  align="center"
                  prop="totalAmount"
                />

                <el-table-column
                  label="已使用金额"
                  align="center"
                  prop="useMoney"
                />
                <el-table-column
                  label="剩余金额"
                  align="center"
                  prop="surpMoney"
                />
              </el-table>
            </div>
            <div style="margin-top: 30px">
              <el-table :data="planInfoList" style="width: 100%" border>
                <el-table-column type="expand">
                  <template slot-scope="props">
                    <el-form
                      label-position="left"
                      inline
                      class="demo-table-expand"
                    >
                      <el-form-item label="税率">
                        <span>{{ props.row.taxRate + "%" }}</span>
                      </el-form-item>
                      <el-form-item label="预算总价">
                        <span>{{ props.row.bdgTotlePrice + "元" }}</span>
                      </el-form-item>
                      <el-form-item label="交付时间">
                        <span>{{ props.row.delvTime }}</span>
                      </el-form-item>
                      <el-form-item label="需求说明">
                        <span>{{ props.row.prodExplain }}</span>
                      </el-form-item>
                      <el-form-item label="采购人">
                        <span>{{ props.row.purchaser }}</span>
                      </el-form-item>
                    </el-form>
                  </template>
                </el-table-column>
                <el-table-column
                  label="预算编号"
                  align="center"
                  prop="bgId"
                  width="120"
                />
                <el-table-column
                  label="产品编号"
                  align="center"
                  prop="prodId"
                  width="180"
                />
                <el-table-column
                  label="产品名称"
                  align="center"
                  prop="prodName"
                  width="120"
                />
                <el-table-column
                  label="规格"
                  align="center"
                  prop="size"
                  width="120"
                />
                <el-table-column
                  label="预算单价"
                  align="center"
                  prop="bdgSinglePrice"
                  width="120"
                />
                <el-table-column
                  label="需求数量"
                  align="center"
                  prop="reqNumber"
                />
              </el-table>
            </div>
            <div class="remark-box" v-show="openStatu < 3">
              <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editor"
                :defaultConfig="toolbarConfig"
                :mode="mode"
              />
              <Editor
                style="height: 500px; overflow-y: hidden"
                v-model="html"
                :defaultConfig="editorConfig"
                :mode="mode"
                @onCreated="onCreated"
              />
            </div>
          </el-tab-pane>
          <el-tab-pane label="审批记录">
            <div>
              <div style="width: 700px; margin: 0 auto">
                <el-steps :active="recordStatu">
                  <el-step title="待提交"></el-step>
                  <el-step title="待审批"></el-step>
                  <el-step title="已生效"></el-step>
                </el-steps>
              </div>
              <el-table :data="recordsList" border style="margin-top: 20px">
                <el-table-column type="expand">
                  <template slot-scope="props">
                    <el-form
                      label-position="left"
                      inline
                      class="demo-table-expand"
                    >
                      <el-form-item label="审批意见">
                        <span v-html="props.row.appRemark"></span>
                      </el-form-item>
                    </el-form>
                  </template>
                </el-table-column>
                <el-table-column
                  label="流程节点名称"
                  align="center"
                  prop="appName"
                />
                <el-table-column
                  label="处理人"
                  align="center"
                  prop="createName"
                />
                <el-table-column
                  label="所属部门"
                  align="center"
                  prop="deptName"
                />
                <el-table-column
                  label="处理时间"
                  align="center"
                  prop="createTime"
                />
                <el-table-column label="状态" align="center" prop="appStatu">
                  <template slot-scope="scope">
                    <dict-tag
                      :options="dict.type.records_statu"
                      :value="scope.row.appStatu"
                    />
                  </template>
                </el-table-column>
              </el-table>

              <pagination
                v-show="recordTotal > 0"
                :total="recordTotal"
                :page.sync="queryRecordParams.pageNum"
                :limit.sync="queryRecordParams.pageSize"
                @pagination="getRecordsList"
              />
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkPlanStatu" v-show="openStatu < 3"
          >同 意</el-button
        >

        <el-button type="danger" v-show="openStatu < 3" @click="backPlan"
          >驳回</el-button
        >
        <el-button @click="checkdialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import utils from "@/api/utils/utils.js";
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import {
  listPurs,
  getPurs,
  delPurs,
  delPursByPlanCode,
  addPurs,
  updatePurs,
  getPlanDetails,
} from "@/api/purs/purs";

import { addRecords, listRecords, updateRecords } from "@/api/purs/records";
export default {
  name: "Purs",
  dicts: ["business_type", "purs_statu", "records_statu"],
  components: { Editor, Toolbar },
  data() {
    return {
      openStatu: 0,
      recordTotal: 0,
      recordStatu: 0,
      editor: null,
      html: "<p></p>",
      toolbarConfig: {},
      editorConfig: { placeholder: "请输入内容..." },
      mode: "default", // or 'simple'

      budgetList: [],
      planInfoList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购计划.审批通过后生成招标项目表格数据
      pursList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: null,
        busiType: null,
        planType: null,
        status: 1,
      },
      queryRecordParams: {
        pageNum: 1,
        pageSize: 10,
        planCode: null,
        appName: null,
        createName: null,
        deptName: null,
        appRemark: null,
        appType: null,
        appStatu: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      activeName: "first",
      checkdialogVisible: false,
      records: {},
      prodPlan: {},
      recordsList: [],
      recordFlag: false,
    };
  },
  created() {},
  mounted() {
    this.getList();
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁编辑器
  },
  methods: {
    backPlan() {
      this.recordFlag = true;
      this.prodPlan = {};
      this.prodPlan.planCode = this.records.planCode;
      this.prodPlan.status = 1;
      updatePurs(this.prodPlan).then((res) => {
        if (res.msg == "操作成功") {
          this.records.appStatu = 1;
          console.log("res:" + JSON.stringify(res));
          this.records.appName = "驳回";
          this.records.createName = sessionStorage.getItem("userName");
          this.records.deptName = sessionStorage.getItem("deptName");
          this.records.appRemark = this.html;
          this.records.appType = 1;
          addRecords(this.records).then((res) => {
            console.log("res:" + JSON.stringify(res));
          });
          this.$modal.msgSuccess("已驳回");
          this.html = "";
          this.checkdialogVisible = false;
          this.getList();
        }
      });
    },

    /** 查询审批计划列表 */
    getRecordsList() {
      listRecords(this.queryRecordParams).then((response) => {
        this.recordsList = response.rows;
        this.recordTotal = response.total;
        this.recordStatu = 0;
        if (this.recordFlag) {
          this.recordStatu = 1;
        } else {
          response.rows.forEach((item) => {
            if (item.appStatu > this.recordStatu) {
              this.recordStatu = parseInt(item.appStatu);
            }
          });
        }
      });
    },
    recordsDetails(row) {
      this.openStatu = 3;
      this.queryRecordParams.planCode = row.planCode;
      this.getRecordsList();
      getPlanDetails(row.planCode).then((res) => {
        this.budgetList = res.data.purProdBudget;
        this.planInfoList = [];
        res.data.purProdBudget.forEach((res) => {
          res.purProdInfos.forEach((item) => {
            this.planInfoList.push(item);
          });
        });
      });
      this.checkdialogVisible = true;
    },

    //审批
    checkPlanStatu() {
      this.records.appName = "采购审批";
      this.records.appType = 1;
      this.recordFlag = false;
      this.records.appRemark = this.html;
      this.records.appStatu = 3;
      console.log("remark:" + this.records.appRemark);
      this.records.createName = sessionStorage.getItem("userName");
      this.records.deptName = sessionStorage.getItem("deptName");
      console.log("records:" + JSON.stringify(this.records));
      this.records.appRemark = this.html;

      addRecords(this.records).then((res) => {
        if (res.msg == "操作成功") {
          this.records = {};
          this.html = "";
          this.prodPlan.status = 3;
          updatePurs(this.prodPlan).then((response) => {});
        }
        this.getList();
        this.checkdialogVisible = false;
      });
    },

    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },

    //审批按钮
    checkPlan(row) {
      this.openStatu = 2;
      this.queryRecordParams.planCode = row.planCode;
      this.getRecordsList();
      this.records.planCode = row.planCode;
      this.prodPlan.planCode = row.planCode;
      getPlanDetails(row.planCode).then((res) => {
        this.budgetList = res.data.purProdBudget;
        this.planInfoList = [];
        res.data.purProdBudget.forEach((res) => {
          res.purProdInfos.forEach((item) => {
            this.planInfoList.push(item);
          });
        });
      });

      this.checkdialogVisible = true;
    },

    handleClose(done) {
      this.checkdialogVisible = false;
    },
    addPurPlan() {
      this.$router.push("/purs/addPurPlan");
    },

    myFormatTime(time) {
      return utils.myformatter(new Date(time));
    },

    handleClick(tab, event) {
      if (tab.name == "first") {
        this.queryParams.status = 1;
      } else if (tab.name == "second") {
        this.queryParams.status = 2;
      } else {
        this.queryParams.status = 3;
      }
      this.getList();
    },
    indexMethod(index) {
      // 根据需要自定义序号生成方式，这里只是简单返回索引+1
      return index + 1;
    },

    /** 查询采购计划.审批通过后生成招标项目列表 */
    getList() {
      this.loading = true;
      listPurs(this.queryParams).then((response) => {
        this.pursList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planCode: null,
        planName: null,
        busiType: null,
        creater: null,
        deptName: null,
        loadFileId: null,
        status: null,
        prodMethod: null,
        planType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.planCode);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购计划.审批通过后生成招标项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$tab.closeOpenPage({
        path: "/update/planInfo",
        query: {
          code: row.planCode,
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.planCode != null) {
            updatePurs(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurs(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planCodes = row.planCode ;
      this.$modal
        .confirm(
          '是否确认删除与采购计划编号为"' +
            planCodes +
            '"有关的所有信息?'
        )
        .then(function () {
          return delPursByPlanCode(planCodes);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "purs/purs/export",
        {
          ...this.queryParams,
        },
        `purs_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped>
.planName:hover {
  text-decoration: underline;
  cursor: pointer;
}
.check-box {
  width: 850px;
  margin: 0 auto;
  overflow: auto;
}

.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  margin-left: 20px;
  width: 50%;
}

.budGetInfo-box {
  width: 820px;
  box-sizing: border-box;
  overflow: auto;
}
.remark-box {
  width: 820px;
  height: 200px;
  border: 1px solid black;
  margin: 50px auto;
  overflow: auto;
}
</style>
